<?php 
if(!($_GET['print'] > 0))
{
	$buttons_l[0]['onmousedown'] = 'edit_form.action = \'print.php'.$commander->getFormAction().'&amp;print=1\'; edit_form.target=\'_blank\';';
	$buttons_l[0]['onmouseup'] = 'edit_form.submit();';
	$buttons_l[0]['image'] = 'print_22.gif';
	
	$buttons_l[1]['image'] = 'tip_22.gif';
	$buttons_l[1]['id'] = 'print_help';
	
	echo $commander->getListCommandsPanel('', '', $buttons_l); 
}
?>
<?php 
if($surveyAdmin->Survey->Demogs)
{
	?>
    <br />
    Valgt demografi kategori:
    <select name="demog_cat" onchange="submit();">
	<?php
	foreach($surveyAdmin->Survey->Demogs as $demog)
	{
		if($demog->Values)
		{
			?>
			<option value="<?php echo $demog->Id; ?>" <?php echo getIsSelectedHTML($demog->Id, $_REQUEST['demog_cat']); ?>><?php echo $demog->Title; ?></option> 
			<?php
		}
	}
	?>
    </select>
    <?php
}
    ?>
<div align="left" class="report statistics page <?php if($_GET['print'] > 0){ echo 'print'; } ?>">
<center>
<h1><?php echo $surveyAdmin->Survey->Title; ?></h1>
<h2><?php echo ucfirst(getWord('summary')); ?></h2>
<h2><?php 
	if($demogs)
	{ echo $surveyAdmin->Survey->getResponsesDemogsCount($demogs); }
	else
	{ echo $surveyAdmin->Survey->getResponsesCount(); }
	echo '&nbsp;'.getWord('responses');
	?></h2>
</center>
</div>
<?php 
if($surveyAdmin->Survey->Sections)
{
	$i = 1;
	foreach($surveyAdmin->Survey->Sections as $section)
	{
		if(($section->Active) && ($section->ActiveQuestions > 0))
		{
			
			?>
            <div align="left" class="report statistics page section <?php if($_GET['print'] > 0){ echo 'print'; } ?>">
            <h2><?php echo $i; ?>.&nbsp;&nbsp;<?php echo $section->Title;  ?></h2>
            <p><?php echo $section->Guide; ?></p>
            <?php 
			if($section->Questions)
			{
				$j=1;
				$xAxis=1;
				$ticks = '';
				$ticks2y = '';
				$label = '';
				
                foreach($section->Questions as $question)
                {
                    if($question->Active)
                    {
						
						$s = 0;
                        if($ticks != '')
                        { $ticks .= ', '; }
                        $ticks .= '[-'.$j.', "'.$j.'.&nbsp;'.str_replace('"','&quot;',substr($question->Question,0,120)).'"]';
                        //$ticks .= '[-'.$j.', "'.ucfirst(getWord('question')).'&nbsp;'.$j.'.&nbsp;"]';
                        if($question->MinValue <= $question->MaxValue)
                        {
                            for($k=$question->MinValue; $k<=$question->MaxValue; $k++)
                            { 
                                if($xAxis < $k)
                                { $xAxis=$k; }
                            }
                            $sumtotal = 0;
                            $totalcount = 0;
                            $count = 0;
                            $average[$j][$s] = 0;
                            for($k = $question->MinValue; $k<=$question->MaxValue; $k++)
                            {	
                                $count = $question->getAnsweredValueCount($k);
                                $total[$k] = ($count*$k);
                                $sumtotal += $total[$k]; 
                                $totalcount += $count;
                            }
                            if($totalcount > 0)
                            { $average[$j][$s] = number_format(($sumtotal/$totalcount),2); }
                            
                            // Graph for each demog
                            if($surveyAdmin->Survey->Demogs)
                            {
                                foreach($surveyAdmin->Survey->Demogs as $demog)
                                {
                                    if(($demog->Values) && (($_REQUEST['demog_cat'] == $demog->Id) || (($_REQUEST['demog_cat'] == '') && ($s == 0))))
                                    {
                                        $s=1;
                                        foreach($demog->Values as $value)
                                        {
                                            $demogs[0] = $value->Id;
                                            $sumtotal = 0;
                                            $totalcount = 0;
                                            $count = 0;
                                            $average[$j][$s] = 0;
                                            for($k = $question->MinValue; $k<=$question->MaxValue; $k++)
                                            {	
                                                $count = $question->getAnsweredValueCount($k, 'value', $demogs);
                                                $total[$k] = ($count*$k);
                                                $sumtotal += $total[$k]; 
                                                $totalcount += $count;
                                            }
                                            if($totalcount > 0)
                                            { $average[$j][$s] = number_format(($sumtotal/$totalcount),2);  }
                                            $label[$s] = $value->Value;
                                            $s++;
                                        }
                                    }
                                }
                            }
                        }
                        if($ticks2y != '')
                        { $ticks2y .= ', '; }
                        $ticks2y .= '[-'.$j.', "<strong>&nbsp;&nbsp;G:'.$average[$j][0];
                        $ticks2y .= '</strong>"]';
                        $j++;
                    }
                }
                ?>
                <div id="legend<?php echo $i; ?>"></div><br /><br />
                <div id="placeholder<?php echo $i; ?>" style="width:640px; height:<?php echo ($j*48)+48 ?>px;"></div><br /><br /><br /><hr /><br />
                <?php
			}
			?>
            <?php if($_REQUEST['v'] < 2): ?>
			<script type="text/javascript">
			jQuery(document).ready(function()
			{
				<?php 
				$data = '';
				$ticks = ereg_replace("\r\n", "", $ticks);
				$ticks = ereg_replace("\n", "", $ticks);
				if($j > 0)
				{
					for($k=1; $k<=$j; $k++)
					{
						if(is_array($average[$k]))
						{
							foreach(array_keys($average[$k]) as $key)
							{
								if($data[$key] != '')
								{ $data[$key] .= ', '; }
								$data[$key] .= '['.$average[$k][$key].', -'.$k.']';
							}
						}
					}
				}
				if($data)
				{
					foreach(array_keys($data) as $key)
					{ 
					?>
					var d<?php echo $key; ?> = [<?php echo $data[$key]; ?>];
					<?php	
					}
				}
				?>
				jQuery.plot(jQuery("#placeholder<?php echo $i; ?>"), [
				{ 
					label: "Gjennomsnitt alle", 
					lines: { show: true },
    				points: { show: true },
					data: d0 
				}
				<?php
				if(count($data) > 1)
				{
					foreach(array_keys($data) as $key)
					{ 
						if(($key > 0) )
						{
						?>
						,{
							label: "<?php echo $label[$key]; ?>",
							lines: { show: true },
							points: { show: true },
							<?php if($key == 1): ?>
							yaxis:2,
							<?php endif; ?>
							data: d<?php echo $key; ?>
						}
						<?php	
						}
					}
				}
				?>
				
				], 
				{ 
					yaxis: 
					{ 
						tickSize: 1,
						ticks: [<?php echo $ticks; ?>],
						labelWidth:250,
						max:-1, 
						min: -<?php echo count($section->Questions); ?>
					}, 
					y2axis: 
					{
						tickSize: 1,
						ticks: [<?php echo $ticks2y; ?>],
						max:-1, 
						min: -<?php echo count($section->Questions); ?>
					},
					xaxis:  
					{ 
						tickSize: 1,
						min: 0, 
						max: <?php echo $xAxis; ?> 
					},
					legend: 
					{ 
						position: 'ne',
						backgroundOpacity:0.4,
						container:jQuery("#legend<?php echo $i; ?>")
					},
					grid: {
						//color: color,
						backgroundColor:null,
						//tickColor: color
						//labelMargin: number
						//markings: array of markings or (fn: axes -> array of markings)
						borderWidth:0,
						//clickable:true,
						hoverable:true
						//autoHighlight:true,
						//mouseActiveRadius:4
					  }

				});
				
				function showTooltip(x, y, contents) 
				{
					jQuery('<div id="tooltip">' + contents + '</div>').css(
					{
						position: 'absolute',
						display: 'none',
						top: y + 5,
						left: x + 5,
						border: '1px solid #fdd',
						padding: '2px',
						'background-color': '#fee',
						opacity: 0.80
					}).appendTo("body").fadeIn(200);
				}


				var previousPoint = null;
				jQuery("#placeholder<?php echo $i; ?>").bind("plothover", function (event, pos, item) 
				{
					jQuery("#x").text(pos.x.toFixed(2));
					jQuery("#y").text(pos.y.toFixed(2));
				
					if (item) {
							if (previousPoint != item.datapoint) 
							{
								previousPoint = item.datapoint;
								jQuery("#tooltip").remove();
								var x = item.datapoint[0].toFixed(2),
									y = item.datapoint[1].toFixed(2);
								
								showTooltip(item.pageX, item.pageY, item.series.label + ":" + x);
							}
						}
						else {
							jQuery("#tooltip").remove();
							previousPoint = null;            
						}
				});

			});
			</script>
            <?php endif; ?>
            </div>
            <?php
			$i++;
		}
	}
}
?>